﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;

namespace University.Workshop.DataAccess
{
    public class CheckoutData : IDisposable
    {
        public const string CnnString = "Data Source=.\\sqlexpress; Initial Catalog=DataBaseHotel;Integrated Security=true";
        private SqlConnection cnn;

        public bool IsToCheckout(int idRoom, DateTime dateTime)
        {
            if (dateTime == null) { throw new ArgumentException("Invalid Input Parameters"); }
            cnn = new SqlConnection(CnnString);
            string sqlSentence = "SELECT idRoom FROM  [DataBaseHotel].[dbo].[TokenRooms] ";
            sqlSentence += "WHERE @date BETWEEN [TokenRooms].[CheckIn]  AND  [TokenRooms].[CheckOut] ";
            sqlSentence += " AND idRoom= @id";

            SqlCommand cmd = cnn.CreateCommand();
            cmd.Parameters.AddWithValue("@date", dateTime);
            cmd.Parameters.AddWithValue("@id", idRoom);
            cmd.CommandText = sqlSentence;
            cmd.CommandType = CommandType.Text;
            try
            {
                cnn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                return reader.HasRows;
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error On Data Base", e);

            }
            finally
            {
                if (cnn.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
        }
       
        #region Miembros de IDisposable

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (cnn != null)
                {
                    cnn.Dispose();
                    cnn = null;
                }
            }
        }

        #endregion
    }
}
